import { Injectable, UnauthorizedException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { getUserDto } from 'src/user/dto/get-user.dto';
import { UserService } from 'src/user/user.service';

@Injectable()
export class AuthService {

    constructor(private readonly userService: UserService, private jwtService: JwtService){}
    async signin(username:string, password:string) {
        const user = await this.userService.find(username)
        if (user && user.password === password) {
            return await this.jwtService.signAsync({
                username: user.username,
                sub: user.id
            })
        }
        return new UnauthorizedException();
    }

    async signup(username:string, password:string) {
        const res = await this.userService.create({ username, password })
        return res;
    }
}
